
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Modify extends JPanel implements ActionListener {
    private JButton backBtn;
    private Atmtest frame;
    private String mima;
    private JTextField testMima;
    private JButton sure;
    private boolean a=false;
private int b = 3;
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

    }

    public Modify(Atmtest frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        testMima = new JTextField();
        testMima.setBounds(180, 500, 200, 30);
        this.add(testMima);

        sure = new JButton(Util.getIcon("/img/buttonSure.jpg"));
        sure.setBounds(900, 400, 100, 50);
        this.add(sure);
        sure.addActionListener(this);

        backBtn = new JButton(Util.getIcon("/img/buttonOff.jpg"));
        backBtn.setBounds(900, 330, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

    }

    public void getmima() {
        this.mima = frame.getUser().getAwd();
    }

    public  void verification(){
        if (testMima.getText().equals(mima)){
            JOptionPane.showMessageDialog(this, "密码正确，请修改密码");
            this.a=true;
        }else {
            b--;
            JOptionPane.showMessageDialog(this, "密码错误");
        }
if (b==0){
    System.exit(0);
}
    }
private void modify(){
        frame.getUser().setAwd(testMima.getText());
    JOptionPane.showMessageDialog(this, "新密码为"+frame.getUser().getAwd());
}

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
        }
        if (sure == e.getSource()){
            if (!a){
                verification();
                testMima.setText("");
                return;
            }
            if (a){
                modify();
            }
        }
    }
}